package com.ruoyi.labsafety.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.labdata.domain.LabFile;
import com.ruoyi.labdata.service.ILabFileService;
import com.ruoyi.labsafety.domain.LabSafetyCheckTask;
import com.ruoyi.labsafety.domain.LabSafetyCheckTaskIndexItem;
import com.ruoyi.labsafety.mapper.LabSafetyCheckTaskIndexItemMapper;
import com.ruoyi.labsafety.service.ILabSafetyCheckTaskIndexItemService;
import com.ruoyi.labsafety.service.ILabSafetyCheckTaskService;
import com.ruoyi.system.utils.FileTypeEnum;

/**
 * 安检检查任务指标项Service业务层处理
 * 
 * @author jun.wang
 * @date 2022-04-25
 */
@Service
public class LabSafetyCheckTaskIndexItemServiceImpl implements ILabSafetyCheckTaskIndexItemService {
	private static final String belongType = FileTypeEnum.checkItem.getValue();
	@Autowired
	private ILabFileService labFileService;
	@Autowired
	private LabSafetyCheckTaskIndexItemMapper labSafetyCheckTaskIndexItemMapper;

	@Autowired
	private ILabSafetyCheckTaskService labSafetyCheckTaskService;

	//@Autowired
	//private ILabSafetyCheckTaskItemFileService labSafetyCheckTaskItemFileService;

	/**
	 * 查询安检检查任务指标项
	 * 
	 * @param id
	 *            安检检查任务指标项ID
	 * @return 安检检查任务指标项
	 */
	@Override
	public LabSafetyCheckTaskIndexItem selectLabSafetyCheckTaskIndexItemById(Long id) {
		return labSafetyCheckTaskIndexItemMapper.selectLabSafetyCheckTaskIndexItemById(id);
	}

	/**
	 * 查询安检检查任务指标项列表
	 * 
	 * @param labSafetyCheckTaskIndexItem
	 *            安检检查任务指标项
	 * @return 安检检查任务指标项
	 */
	@Override
	public List<LabSafetyCheckTaskIndexItem> selectLabSafetyCheckTaskIndexItemList(
			LabSafetyCheckTaskIndexItem labSafetyCheckTaskIndexItem) {
		return labSafetyCheckTaskIndexItemMapper.selectLabSafetyCheckTaskIndexItemList(labSafetyCheckTaskIndexItem);
	}

	/**
	 * 新增安检检查任务指标项
	 * 
	 * @param labSafetyCheckTaskIndexItem
	 *            安检检查任务指标项
	 * @return 结果
	 */
	@Override
	public int insertLabSafetyCheckTaskIndexItem(LabSafetyCheckTaskIndexItem labSafetyCheckTaskIndexItem) {
		return labSafetyCheckTaskIndexItemMapper.insertLabSafetyCheckTaskIndexItem(labSafetyCheckTaskIndexItem);
	}

	/**
	 * 修改安检检查任务指标项
	 * 
	 * @param labSafetyCheckTaskIndexItem
	 *            安检检查任务指标项
	 * @return 结果
	 */
	@Override
	public int updateLabSafetyCheckTaskIndexItem(LabSafetyCheckTaskIndexItem labSafetyCheckTaskIndexItem) {
		return labSafetyCheckTaskIndexItemMapper.updateLabSafetyCheckTaskIndexItem(labSafetyCheckTaskIndexItem);
	}

	/**
	 * 批量删除安检检查任务指标项
	 * 
	 * @param ids
	 *            需要删除的安检检查任务指标项ID
	 * @return 结果
	 */
	@Override
	public int deleteLabSafetyCheckTaskIndexItemByIds(Long[] ids) {
		for(Long id : ids){
			labFileService.deleteLabFile(id, belongType);
		}
		return labSafetyCheckTaskIndexItemMapper.deleteLabSafetyCheckTaskIndexItemByIds(ids);
	}

	/**
	 * 删除安检检查任务指标项信息
	 * 
	 * @param id
	 *            安检检查任务指标项ID
	 * @return 结果
	 */
	@Override
	public int deleteLabSafetyCheckTaskIndexItemById(Long id) {
		return labSafetyCheckTaskIndexItemMapper.deleteLabSafetyCheckTaskIndexItemById(id);
	}

	@Override
	public void insertLabSafetyCheckTaskIndexItemByCheckFormId(Long instanceId, Long checkFormId) {
		labSafetyCheckTaskIndexItemMapper.insertLabSafetyCheckTaskIndexItemByCheckFormId(instanceId, checkFormId);
	}

	@Override
	public int updateLabSafetyCheckTaskIndexItemStatus(LabSafetyCheckTaskIndexItem taskIndexItem) {

		return labSafetyCheckTaskIndexItemMapper.updateLabSafetyCheckTaskIndexItemStatus(taskIndexItem.getId(),
				taskIndexItem.getStatus());
	}

	@Override
	public List<LabSafetyCheckTaskIndexItem> getTreeTableInfo(Long instanceId) {

		LabSafetyCheckTask labSafetyCheckTask = labSafetyCheckTaskService.selectLabSafetyCheckTaskById(instanceId);

		return labSafetyCheckTask.getIndexItemList();

	}

	@Override
	public int updateLabSafetyCheckTaskIndexItemHiddenDanger(LabSafetyCheckTaskIndexItem taskIndexItem) {
		if (taskIndexItem.getCheckedStatus() != null) {
			return labSafetyCheckTaskIndexItemMapper.updateLabSafetyCheckTaskIndexItemCheckedStatus(
					taskIndexItem.getId(), taskIndexItem.getCheckedStatus());
		}
		return labSafetyCheckTaskIndexItemMapper.updateLabSafetyCheckTaskIndexItemHiddenDanger(taskIndexItem.getId(),
				taskIndexItem.getHiddenDanger());
	}

	@Override
	public int insertLabSafetyCheckTaskIndexItemFile(LabSafetyCheckTaskIndexItem labSafetyCheckTaskIndexItem) {
		int ret = 1;
		//if (labSafetyCheckTaskIndexItem.getRemark() != null) {
		ret = this.labSafetyCheckTaskIndexItemMapper.updateLabSafetyCheckTaskIndexItem(labSafetyCheckTaskIndexItem);
		//}
		if(labSafetyCheckTaskIndexItem.getSyncStatus()==1){
			updateLabSafetyCheckTaskIndexItemSyncStatus(labSafetyCheckTaskIndexItem);
		}
		Long id = labSafetyCheckTaskIndexItem.getId();
		labFileService.updateFiles(labSafetyCheckTaskIndexItem.getFiles(), id, belongType);
		return ret;
	}

	@Override
	public List<LabFile> selectLabSafetyCheckTaskIndexItemFileById(Long id) {
		LabFile query = new LabFile();
		query.setInstrumentId(id);
		query.setBelongType(this.belongType);
		return labFileService.selectLabFileList(query);
	}

	@Override
	public List<LabSafetyCheckTaskIndexItem> selectLabSafetyCheckTaskIndexItemByParentId(Long parentId) {
		return labSafetyCheckTaskIndexItemMapper.selectLabSafetyCheckTaskIndexItemByParentId(parentId);
	}

	@Override
	@Transactional
	public int updateLabSafetyCheckTaskIndexItemSyncStatus(LabSafetyCheckTaskIndexItem taskIndexItem) {
		Long id = taskIndexItem.getId();
		Integer syncStatus = taskIndexItem.getSyncStatus();
		int i = labSafetyCheckTaskIndexItemMapper.updateLabSafetyCheckTaskIndexItemSyncStatus(id, syncStatus);
		if (syncStatus.intValue() == 1) {
			LabSafetyCheckTaskIndexItem indexItem = labSafetyCheckTaskIndexItemMapper
					.selectLabSafetyCheckTaskIndexItemById(id);
			labSafetyCheckTaskIndexItemMapper.synchUpdateChildStatus(indexItem);
		/*	updateChildHiddenDangerAndCheckedStatus(indexItem.getIndexItemId(), indexItem.getCheckedStatus(),
					indexItem.getHiddenDanger());*/
		}
		return i;
	}

	@Override
	public void insertLabSafetyCheckTaskIndexItemByCheckTaskId(Long instanceId, Long checkTaskId) {
		LabSafetyCheckTaskIndexItem query = new LabSafetyCheckTaskIndexItem();
		query.setInstanceId(checkTaskId);
		query.setStatus(0);
		List<LabSafetyCheckTaskIndexItem> list = labSafetyCheckTaskIndexItemMapper
				.selectLabSafetyCheckTaskIndexItemList(query);

		for (LabSafetyCheckTaskIndexItem item : list) {
			Long itemId = item.getId();
			item.setInstanceId(instanceId);
			item.setId(null);
			// 插入指标
			labSafetyCheckTaskIndexItemMapper.insertLabSafetyCheckTaskIndexItem(item);
			// 插入指标文件
			labFileService.updateFiles(item.getFiles(), itemId, belongType);
		}
	}

	/*public void updateChildHiddenDangerAndCheckedStatus(Long instanceId,Long parentId, Integer checkedStatus, Integer hiddenDanger) {
		List<LabSafetyCheckTaskIndexItem> list = labSafetyCheckTaskIndexItemMapper
				.selectLabSafetyCheckTaskIndexItemByParentId(parentId);
		if (CollectionUtils.isEmpty(list)) {
			return;
		}
		for (LabSafetyCheckTaskIndexItem taskIndexItem : list) {
			taskIndexItem.setCheckedStatus(checkedStatus);
			taskIndexItem.setHiddenDanger(hiddenDanger);
			labSafetyCheckTaskIndexItemMapper.updateLabSafetyCheckTaskIndexItem(taskIndexItem);

			//updateChildHiddenDangerAndCheckedStatus(taskIndexItem.getIndexItemId(), checkedStatus, hiddenDanger);
		}
	}*/
	
	public Integer getTaskHiddenDanger(Long instanceId){
		return labSafetyCheckTaskIndexItemMapper.getTaskHiddenDanger(instanceId);
	}

	@Override
	public List<LabSafetyCheckTaskIndexItem> selectLabSafetyCheckTaskIndexItemByNo(String itemNo) {
		return labSafetyCheckTaskIndexItemMapper.selectLabSafetyCheckTaskIndexItemByNo(itemNo);
	}
}
